package com.digitaldreamers.ddmonsters.scene;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.IEntity;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.content.Context;

import com.digitaldreamers.ddmonsters.ResourceManager;
import com.digitaldreamers.framework.BaseResourceManager;
import com.digitaldreamers.framework.activity.GameActivity;
import com.digitaldreamers.framework.scene.ManagedNetworkScene;

public abstract class BaseNetworkScene extends ManagedNetworkScene {
	public BaseResourceManager mBaseResourceManager;
	public ResourceManager mResourceManager;
	public Context mContext;
	public GameActivity mActivity;
	public Engine mEngine;
	public Camera mCamera;
	public VertexBufferObjectManager mVBOM;
	public float mCameraWidth;
	public float mCameraHeight;
	public float mCameraMiddleX;
	public float mCameraMiddleY;
	public float mCameraScaleFactorX;
	public float mCameraScaleFactorY;
	
	public BaseNetworkScene() {
		this(0f);
	}
	
	public BaseNetworkScene(float pMinLoadingScreenTime) {
		super(pMinLoadingScreenTime);
		mBaseResourceManager = BaseResourceManager.getInstance();
		mResourceManager = ResourceManager.getInstance();
		mContext = mBaseResourceManager.mContext;
		mActivity = mBaseResourceManager.mActivity;
		mEngine = mBaseResourceManager.mEngine;
		mCamera = mEngine.getCamera();
		mVBOM = mEngine.getVertexBufferObjectManager();
		mCameraWidth = mBaseResourceManager.mCameraWidth;
		mCameraHeight = mBaseResourceManager.mCameraHeight;
		mCameraMiddleX = mCameraWidth / 2;
		mCameraMiddleY = mCameraHeight / 2;
		mCameraScaleFactorX = mBaseResourceManager.mCameraScaleFactorX;
		mCameraScaleFactorY = mBaseResourceManager.mCameraScaleFactorY;
	}
	
	@Override
	public void onUnloadScene() {
		super.onUnloadScene();
		int childCount = getChildCount();
		for(int i = 0; i < childCount; i++) {
			final IEntity child = getChildByIndex(i);
			mEngine.runOnUpdateThread(new Runnable() {
				@Override
				public void run() {
					detachChild(child);
					child.dispose();
				}
			});
		}
	}
}
